import { http } from '@/request/http'
import type { RoleVO } from '@/request/api/role'
import type { PageDTO, PageVO } from '@/type/type'
import type { MakeOptional } from '@/type'

export type BaseRouter = {
  // 主键（自增）
  id: number
  // 角色ID
  roleId: number
  // 路由标题
  title: string
  // 路由层级
  level: number
  // 次序
  priority: number
  // 父路由ID
  parentId: number
  // 路由类型(true Admin, false: ui)
  isAdmin: boolean
  // 是否导航栏路由
  isBar: boolean
  // 是否外链
  isFrame: boolean
  // 路由地址
  path: string
  // 路由前缀图标
  icon: string
  // 路由后缀图标
  name: string
  // 路由组件路径
  component: string
  // 是否启用 状态
  status: boolean
}

export type RouterDTO = MakeOptional<BaseRouter>

export type RouterVO = BaseRouter & {
  // 角色
  role: RoleVO
  // 子路由
  children: RouterVO[]
}

export const initRouterDTO = (): RouterDTO => {
  return {
    id: undefined,
    roleId: undefined,
    title: undefined,
    level: undefined,
    priority: undefined,
    parentId: undefined,
    isAdmin: undefined,
    isBar: undefined,
    isFrame: undefined,
    path: undefined,
    icon: undefined,
    name: undefined,
    component: undefined,
    status: undefined
  }
}

export const getRouterListApi = () => {
  return http<RouterVO[]>({
    url: '/router/list',
    method: 'GET'
  })
}

export const getRouterListByIsAdminApi = (isAdmin: boolean) => {
  return http<BaseRouter[]>({
    url: `/admin/router/level/${isAdmin}`,
    method: 'GET'
  })
}

export const getRouterAdminPageApi = (dto: PageDTO) => {
  return http<PageVO<RouterVO>>({
    url: '/admin/router/page',
    method: 'GET',
    params: {
      dto: JSON.stringify(dto)
    }
  })
}

// 新增
export function addRouterApi(data: RouterDTO) {
  return http<boolean>({
    url: '/admin/router/insert',
    method: 'POST',
    data: data
  })
}

// 修改
export function updateRouterApi(data: RouterDTO) {
  return http<boolean>({
    url: '/admin/router/update',
    method: 'PUT',
    data: data
  })
}

// 删除
export function delRouterApi(ids: number[]) {
  return http<boolean>({
    url: '/admin/router/delete',
    method: 'DELETE',
    data: ids
  })
}
